﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace MaterialTypeRecognition.Shell.EntityFrameworkCore
{
    /// <summary>
    /// 支持使用命令行迁移。
    /// </summary>
    public class WebMiddlewareDbContentFactory : IDesignTimeDbContextFactory<MyAppDbContext>
    {
        public MyAppDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<MyAppDbContext>();

            return new MyAppDbContext(optionsBuilder.Options, BuildConfiguration());
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            return new ConfigurationBuilder()
             .SetBasePath(Directory.GetCurrentDirectory())
             .AddJsonFile("appsettings.json")
             .Build();
        }
    }
}
